package com.jiang.jetpackdemo.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayoutMediator
import com.jiang.jetpackdemo.R
import com.jiang.jetpackdemo.fragment.FavoritesPicFragment
import kotlinx.android.synthetic.main.activity_favorites_page.*

class FavoritesPageActivity : AppCompatActivity() {
    private val fragmentStateAdapter by lazy {
        object : FragmentStateAdapter(this) {
            override fun getItemCount(): Int = 3
            override fun createFragment(position: Int): Fragment {
                val type = when (position) {
                    0 -> FavoritesPicFragment.Type.VIDEO
                    1 -> FavoritesPicFragment.Type.PICTURE
                    else -> FavoritesPicFragment.Type.NEWS
                }
                return FavoritesPicFragment.newInstance(type)
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_favorites_page)

        initView()
    }

    private fun initView() {
        vp.adapter = fragmentStateAdapter

        TabLayoutMediator(tabLayout, vp) { tab, position ->
            tab.text = when (position) {
                0 -> "Videos"
                1 -> "Pictures"
                else -> "News"
            }
           // Toast.makeText(this, "$position", Toast.LENGTH_SHORT).show()
        }.attach()
    }

}